// personal_center/pages/add_abroad_address/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    country: '',
    recipient_country: '',
    checked: true,
    recipient_zip: '',
    recipient_name: '',
    recipient_phone: '',
    recipient_state: '',
    recipient_city: '',
    recipient_address: '',
    recipient_district:'',
    id: '',
    type:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      this.setData({
          type:options.type
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  get_country_list(e) {
    let countyr_list = e.detail.value;
    if (wx.getStorageSync('address_item0')) {
      let data = wx.getStorageSync('address_item0');
      this.setData({
        recipient_country: countyr_list.find(item => {return item.name === data.recipient_country }).code,
        country:data.recipient_country,
        recipient_zip:data.recipient_zip,
        recipient_address:data.recipient_address,
        recipient_city:data.recipient_city,
        recipient_name:data.recipient_name,
        recipient_phone:data.recipient_phone,
        recipient_state:data.recipient_state,
        id:data.id,
        default:data.default
      })
    }
  },
  onChange(event) {
    this.setData({
      checked: event.detail,
    });
  },
  show_country() {
    this.selectComponent('#country').show_demo()
  },
  get_country(e) {
    this.setData({
      recipient_country: e.detail.value,
      country: e.detail.title
    })
  },
  get_zip(e) {
    this.setData({
      recipient_zip: e.detail.value
    })
  },
  get_name(e) {
    this.setData({
      recipient_name: e.detail.value
    })
  },
  get_phone(e) {
    this.setData({
      recipient_phone: e.detail.value
    })
  },
  get_state(e) {
    this.setData({
      recipient_state: e.detail.value
    })
  },
  get_city(e) {
    this.setData({
      recipient_city: e.detail.value
    })
  },
  get_address(e) {
    this.setData({
      recipient_address: e.detail.value
    })
  },
  get_district(e){
    this.setData({
      recipient_district: e.detail.value
    })
  },
  confirm() {
    const that = this;
    if (!that.data.recipient_country) {
      app.toast('请选择国家')
    } else if (!that.data.recipient_zip) {
      app.toast('请输入邮编')
    } else if (!that.data.recipient_name) {
      app.toast('请输入姓名')
    } else if (!that.data.recipient_phone) {
      app.toast('请输入电话')
    } else if (!that.data.recipient_state) {
      app.toast('请输入州')
    } else if (!that.data.recipient_city) {
      app.toast('请输入城市')
    } else if (!that.data.recipient_address) {
      app.toast('请输入详细地址')
    } else {
      app.loading()
      let obj = {
        recipient_country: that.data.recipient_country,
        recipient_zip: that.data.recipient_zip,
        recipient_name: that.data.recipient_name,
        recipient_phone: that.data.recipient_phone,
        recipient_phone: that.data.recipient_phone,
        recipient_state: that.data.recipient_state,
        recipient_city: that.data.recipient_city,
        recipient_address: that.data.recipient_address,
        type: that.data.type,
        default:that.data.checked?1:0
      }
      if (that.data.id) {
        obj.id = that.data.id
      }
      app.wxRequest('/api/Address/add', obj, function (res) {
        if (res.code == 200) {
          app.toast('添加成功')
          wx.navigateBack({
            delta: 1
          })
        } else {
          app.toast(res.message)
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})